//Constants
var minSize = 2;
var maxSize = 7;
//variables
var editedQuestion = null;
//Edits question
function makeEditable() {
	$('a.edit').click(function(e){
		e.preventDefault();
		editedQuestion = $(this).parent().parent();
		openQuestion();
	});
}
function makeDelable()
{
	$('a.delete').click(function(e) 
	{
		e.preventDefault();
		$(this).parent().parent().remove();
	});
}

function makeDelableAnswer()
{
	$('a.delete').click(function(e) 
	{
		e.preventDefault();
		if ($('ul.answers li').length > minSize)
		{
			if($(this).parent().find('input[type=radio]').is(':checked'))
				{
					$('input[type=radio]:eq(0)').attr('checked', 'checked');
				}
			$(this).parent().remove();
		}
	});
}

//Adds empty answer
function addAnswer(str) {
		if(!str) str = '';
		if ($('ul.answers li').length < maxSize) {
		$('ul.answers').append('<li><span class="commentArea"><input type="radio" name="correct" value="0" /></span><span class="magicInput"><input class="magicInput" type="text" name="answer" value="' + str + '"/></span><a class="delete magicInput">x</a></li>');
		makeDelableAnswer();
	}
}
//Opens question editing window for a new question
function newQuestion() {
		editedQuestion = null;
		$('input[name="Qname"]').val($('input[name="newQname"]').val());
		$('ul.answers').empty();
		addAnswer();
		addAnswer();
		$('input[type=radio]:eq(0)').attr('checked', 'checked');
		$.fancybox.open('#fancy');
}

function openQuestion() {
	$('input[name="Qname"]').val($(editedQuestion).find('h2').find('span').text());
	$('ul.answers').empty();
	$(editedQuestion).find('li').each(function(j) {
		addAnswer($.trim($(this).text()));
	});
	$('input[type=radio]:eq('+ parseInt($(editedQuestion).find('input[type=hidden]').val()) +')').attr('checked', 'checked');
	$.fancybox.open('#fancy');
}

function correctAnswerIndex() {
	var radioButtons = $("form.writeQ input:radio");
	var selectedIndex = radioButtons.index(radioButtons.filter(':checked'));
	return selectedIndex;
}

function answers2Li() {
	var answers = '<ol class="innerList">\n';
	var correct = correctAnswerIndex();
	$('.answers input[type="text"]').each(function(i) {
		if(i != correct)
			answers += '<li>' + $(this).val() + '</li>';
		else
			answers += '<li class="correct"><img class="icon" src="img/correct.png" alt="correct answer checkmark" />' + $(this).val() + '</li>';
	});
	answers +='<input type="hidden" name="index" value="' + correct + '"/>'
	answers += '</ol>\n';
	return answers;
}
//When you click add, it opens a pop to create a new question
$(function(){
	makeEditable();
	makeDelable();
	
	$('a.addQ').click(function(e){
		e.preventDefault();
		newQuestion();
	});
//When you choose a question and hit enter, it opens the same pop up
	$('form.addQ').submit(function(e) {
		e.preventDefault();
		newQuestion();
	});
//Adds question to list when you hit add
	$('a.listQ').click(function(e){
		e.preventDefault();
		if(editedQuestion == null){
			$('ul#questions').prepend('<li><h2><span>' + $('input[name="Qname"]').val() + '</span><a class="delete magicInput" href="">x</a><a class="edit magicInput" href="">edit</a></h2>' + answers2Li() + '</li>');
			$('input[name="newQname"]').val('');
		}
		else
			$(editedQuestion).html('<h2><span>' + $('input[name="Qname"]').val() + '</span><a class="delete magicInput" href="">x</a><a class="edit magicInput" href="">edit</a></h2>' + answers2Li());
		$.fancybox.close();
		makeEditable();
		makeDelable();
	});

//Adds an empty answer
	$('a.addAnswer').click(function(e){
		e.preventDefault();
		addAnswer();
	});
//Saves data to paragraph - good
	$('a.saveAll').click(function(e){
		e.preventDefault();
		var dic = {};
		$('#questions>li').each(function(i) {
			dic['name' + (i + 1)] = $(this).find('h2').find('span').text();
			dic['rightAnswer' + (i + 1)] = $(this).find('input:hidden').val();
			$(this).find('li').each(function(j) {
				dic['answer' + (i + 1) + '_' + (j + 1)] = $(this).text();
			});
		});
		dic['key'] = $('input[name=lectureKey]').val();
		console.log($.param(dic));
		$.post("setQuestions", dic).success(function() {top.location.href="lecture?key=" + $('input#key').val();});
	});
});

/*
var radioButtons = $("#myFormID input:radio[name='radioFieldName']");
var selectedIndex = radioButtons.index(radioButtons.find(':checked'));
*/